<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class common_model extends CI_Model {
	function __construct(){
		parent::__construct();
	}
	
	
	/**
	 * 우편번호 처리
	 *
	 * @access	public
	 * @return	result
	 */
	function select_post(){
		$dong = $this->input->get('dong');
		if($dong){
			$this->db->select("*");
			$this->db->like('dong', $dong);
			//$this->db->where('master_id', $id);
			$query = $this->db->get('ch_zipcode');
			return $query->result();
		}else{
			return null;
		}
	}
	
	/**
	 * 차일드 코드 조회
	 *
	 * @access	public
	 * @param   master_id
	 * @return	result
	 */
	function select_child_code($master_id){
		$this->db->select("*");
		$this->db->where(array('open_yn'=>'Y','del_yn'=>'N','master_id'=>$master_id));
		$this->db->order_by('sort','asc');
		$query = $this->db->get('ch_code_child');
		//echo $this->db->last_query();
		return $query->result();
	}
	
	/**
	 * uri 지정 배열 위치까지 가져오기
	 *
	 * @param integer
	 * @return string : /first/second
	 */
	function get_index_uri($index){
		$this->load->helper('url');
		$uri = null;
		foreach($this->uri->segment_array() as $key => $value){
			$seg[] = $this->uri->segment($key);
			if($key == $index){
				break;	
			}
		}
		$uri = implode('/',$seg);
		return $uri;
	}
	
	/**
	 * customizing paginaton
	 *
	 * @param array
	 * @return string 
	 */
	function pagination($config){
		$page_link = null;
		$block = ceil($config['page'] / $config['listnum']);
		$first = ($block -1)*$config['listnum'];
		$last = $block *$config['listnum'];
		$total_page = ceil($config['total_rows'] / $config['listnum']);
		$total_block = ceil($total_page / $config['listnum']);
		
		if($block >= $total_block){
			$last = $total_page;
		}
		
		//첫페이지
		$link = ($block > 1) ? $config['base_url'].'/1' : '#';
		$page_link .= "<a href=\"{$link}\"><img src=\"/theme/{$config['theme']}/images/btn_first.gif\" alt=\"처음 페이지로\" /></a>\n";
		//이전페이지
		$link = ($config['page'] > 1) ? $config['base_url'].'/'.($config['page']-1) : '#';
		$page_link .= "<a href=\"{$link}\"><img src=\"/theme/{$config['theme']}/images/btn_prev.gif\" alt=\"이전 페이지로\" style=\"padding-right:20px;\" /></a>\n";
				
		$page_number = '';
		for($i=$first+1; $i <= $last; $i++){
			$css = ($config['page'] == $i) ? 'on' : null; 
			$page_number .= "<span class=\"numbers\"><a href=\"{$config['base_url']}/{$i}\" class=\"{$css}\">{$i}</a></span>\n";
		}
		$page_link .= $page_number;
		 
		//다음페이지
		$link = ($total_page > $config['page']) ? $config['base_url'].'/'.($config['page']+1) : '#';
		$page_link .= "<a href=\"{$link}\"><img src=\"/theme/{$config['theme']}/images/btn_next.gif\" alt=\"다음 페이지로\" style=\"padding-left:20px;\" /></a>\n";
		//마지막페이지
		$link = ($block < $total_block) ? $config['base_url'].'/'.$total_page : '#';
		$page_link .= "<a href=\"{$link}\"><img src=\"/theme/{$config['theme']}/images/btn_end.gif\" alt=\"마지막 페이지로\" /></a>\n";
		
		
		return $page_link;
	}
}
?>